<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\JxtAppointment;
use App\Models\JxtArticle;
use App\Models\JxtBanner;
use Illuminate\Support\Facades\Validator;

class AppointmentController extends Controller
{
    protected function validateCreateItem(array $data): \Illuminate\Contracts\Validation\Validator
    {
        return Validator::make($data, [
            'username' => 'required',
            'mobile' => 'required|numeric',
            'address_home' => 'required'
        ], [
            'username.required' => '请输入姓名',
            'mobile.required' => '请输入手机号',
            'mobile.numeric' => '请输入正确的手机号',
            'address_home.required' => '请输入家庭地址',
        ]);
    }

    public function createAppointment(): \Illuminate\Http\JsonResponse
    {
        $validator = $this->validateCreateItem(request()->all());
        if ($validator->fails()) {
            $message = $validator->errors()->first();
            return response()->json([
                'code' => 1,
                'message' => $message
            ]);
        }
        JxtAppointment::create([
            'username' => request('username'),
            'mobile' => request('mobile'),
            'address_home' => request('address_home'),
            'address_school' => request('address_school')
        ]);

        return response()->json([
            'message' => 'success',
            'code' => 0,
            'status_code' => 200
        ]);
    }
}
